package com.bcn.login_mybatis_demo.service.serviceImpl;

import com.bcn.login_mybatis_demo.dto.RoleDTO;
import com.bcn.login_mybatis_demo.mapper.RoleMapper;
import com.bcn.login_mybatis_demo.pojo.Role;
import com.bcn.login_mybatis_demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Role> getRoleList(RoleDTO roleDTO) {
        // 如果没有传递 pageNo，设置为 1（第一页）
        if (roleDTO.getPageNo() == null || roleDTO.getPageNo() <= 0) {
            roleDTO.setPageNo(1);
        }
        // 如果没有传递 pageSize，设置为 10（每页10条数据）
        if (roleDTO.getPageSize() == null || roleDTO.getPageSize() <= 0) {
            roleDTO.setPageSize(10);
        }
        int offset = (roleDTO.getPageNo() - 1) * roleDTO.getPageSize();
        int pageSize = roleDTO.getPageSize();
        return roleMapper.selectRole(roleDTO, offset, pageSize);
    }

    @Override
    public long getRoleCount(RoleDTO roleDTO){
        return roleMapper.countRole(roleDTO);
    }

    @Override
    public String createRole(Role role){
        Role roleItem = roleMapper.selectRoleByName(role.getName());
        if(roleItem != null){
            return "角色名称已存在，请修改名称";
        }else {
            roleMapper.createRole(role);
            return "SUCCESS";
        }
    }

    @Override
    public String deleteRoleById(int id){
        Role role = roleMapper.selectRoleById(id);
        if(role != null){
            roleMapper.deleteRoleById(id);
            return "SUCCESS";
        }
        return "角色不存在";
    }

    @Override
    public Role getRoleByID(int id){
        return roleMapper.selectRoleById(id);
    }

    @Override
    public String updateRole(Role role){
        roleMapper.updateRole(role);
        return "SUCCESS";
    }
}
